package cn.dglydrpy.study.algorithm.od;

import java.util.*;

/**
 * @author Yyy
 * @date 2024/11/13 21:53
 * @description
 */
public class Main3 {
    public static void main(String[] args) {
        Scanner scanner = new Scanner(System.in);
        while (scanner.hasNextLine()){
            String[] inputs = scanner.nextLine().split(",");
            int[] arr = new int[inputs.length];
            int n = 0;
            Map<Integer,Integer> map = new HashMap();
            for(int i=0;i<arr.length;i++){
                arr[i] = Integer.valueOf(inputs[i]);
                if(arr[i] < 1 || arr[i] > 9){
                    System.out.print(-1);
                    return;
                }

                int temp = arr[i];
                if(arr[i] == 5){
                    arr[i] = 2;
                }
                if(arr[i] == 9){
                    arr[i] = 6;
                }
                if(map.containsKey(arr[i])){
                    System.out.print(-1);
                    return;
                }else{
                    map.put(arr[i],1);
                }
                
                n = Math.max(n,temp);
            }
            int ans = myAnswer(arr,n);
            System.out.println(ans);
        }
    }

    private static int myAnswer(int[] arr,int n) {
        ArrayList<Integer> list = new ArrayList<>();
        for(int i : arr){
            
            list.add(i);
            if(i == 2){
                list.add(5);
            }
            if(i == 6){
                list.add(9);
            }
        }
        
        Collections.sort(list);
        
        
        // 造多位数
        for(int i=0;i<list.size();i++){
            for(int j=1;j<list.size();j++){
                list.add(list.get(i)*10 + list.get(j));
            }
        }
        
        
        int ans = 0;
        
        Queue<Integer> queue = new LinkedList<>();
        for(int i : list){
            queue.add(i);
        }
        
        while (n > 0){
            ans = queue.poll();
            n--;
        }
        
        return ans;
    }
    
}
